﻿using Fast.ExceptionHandling;
using System;
using System.Collections;

namespace Fast.Extensions
{
    /// <summary>
    /// 异常扩展
    /// </summary>
    [SkipScan]
    public static class FastExceptionExtensions
    {
        /// <summary>
        /// 调用string.IsNullOrWhiteSpace()验证
        /// </summary>
        /// <param name="str"></param>
        /// <param name="message"></param>
        /// <returns></returns>
        /// <exception cref="UserFriendlyException"></exception>
        public static string ThrowIfNullOrEmpty(this string str, string message = "参数为空异常")
        {
            if (string.IsNullOrWhiteSpace(str))
            {
                throw new UserFriendlyException(message);
            }

            return str;
        }

        /// <summary>
        /// 验证实体是否为null
        /// </summary>
        /// <typeparam name="T">实体类型</typeparam>
        /// <param name="entity">实体</param>
        /// <param name="message">错误信息</param>
        /// <returns></returns>
        /// <exception cref="UserFriendlyException"></exception>
        public static T ThrowIfNull<T>(this T entity, string message = "参数为空异常") where T : class, new()
        {
            var collection = entity as ICollection;
            if (collection != null && collection.Count == 0)
            {
                throw new UserFriendlyException(message);
            }

            if (entity == null)
            {
                throw new UserFriendlyException(message);
            }

            return entity;
        }

        /// <summary>
        /// 自定义实体验证约束规则并验证
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="entity"></param>
        /// <param name="predicate"></param>
        /// <param name="message"></param>
        /// <returns></returns>
        /// <exception cref="UserFriendlyException"></exception>
        public static T ThrowIf<T>(this T entity, Predicate<T> predicate, string message)
        {
            if (predicate(entity))
            {
                throw new UserFriendlyException(message);
            }
            return entity;
        }

        /// <summary>
        /// 自定义实体验证属性是否为空
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <typeparam name="P"></typeparam>
        /// <param name="entity"></param>
        /// <param name="property"></param>
        /// <param name="propertyName"></param>
        /// <returns></returns>
        /// <exception cref="UserFriendlyException"></exception>
        public static T ThrowIfPropertyIsNull<T, P>(this T entity, Func<T, P> property, string propertyName)
            where T : class, new()
        {
            entity.ThrowIfNull();
            if (property(entity) == null)
            {
                throw new UserFriendlyException($"参数 {propertyName} 不能为空");
            }
            return entity;
        }

        /// <summary>
        /// 自定义实体验证属性是否为空
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <typeparam name="P"></typeparam>
        /// <param name="entity"></param>
        /// <param name="property"></param>
        /// <param name="propertyName"></param>
        /// <returns></returns>
        /// <exception cref="UserFriendlyException"></exception>
        public static T ThrowIfPropertyIsNullOrEmpty<T>(this T entity, Func<T, string> property, string propertyName)
            where T : class, new()
        {
            entity.ThrowIfNull();
            if (string.IsNullOrWhiteSpace(property(entity)))
            {
                throw new UserFriendlyException($"参数 {propertyName} 不能为空");
            }
            return entity;
        }
    }
}
